package com.hjs.community.search.listener;

import com.alibaba.fastjson.JSON;
import com.hjs.community.search.dto.PostDTO;
import com.hjs.community.search.service.SearchPostService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class PostListener {

    @Autowired
    private SearchPostService postService;

    @KafkaListener(topics = "post-index")
    public void consumerPostTopic(String json) {
        PostDTO postDTO = JSON.parseObject(json, PostDTO.class);
        try {
            log.debug("postDto:{}", postDTO);
            System.out.println("json = " + postDTO);
            postService.addPost(postDTO);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
